import React from 'react';
import { inject, observer } from 'mobx-react';

import E9Login from './e9login/E9Login';
import { getQueryParams } from '../../util/loginUtil';

@inject('portalLoginStore')
@observer
class Login extends React.Component {
  componentWillMount() {
    const { portalLoginStore } = this.props;
    const { state = {} } = portalLoginStore;
    const { templateType } = state;
    // 加载自定义登录组件
    if (templateType == 'ECustom') {
      const { layoutData = {} } = state;
      portalLoginStore.onLoadECustomLogin(layoutData);
    }
    const queryParams = getQueryParams();
    portalLoginStore.setQueryParams(queryParams);
    portalLoginStore.getUpgradeInfo(queryParams);
  }

  componentWillReceiveProps(nextProps) {
    const { portalLoginStore } = nextProps;
    const queryParams = getQueryParams();
    portalLoginStore.setQueryParams(queryParams);
  }

  render() {
    const { portalLoginStore } = this.props;
    const { updatestatus, casstatus, state = {}, customloading, beforLoginRenderEventStatus } = portalLoginStore;
    const { id, templateType, layoutData = {} } = state;

    if (updatestatus != '0' || casstatus != '0' || !id || !beforLoginRenderEventStatus) {
      return <div />;
    }

    if (templateType == 'ECustom') {
      const { layoutId, layoutType, layoutCustom, layoutConfig = {}, layoutUUID } = layoutData;

      if (layoutCustom == '1') {
        // 使用自定义登录组件
        if (customloading && window[layoutConfig.module]) {
          const ECustomLogin = window[layoutConfig.module].com[layoutConfig.component];
          return <ECustomLogin ecId={`${(this && this.props && this.props.ecId) || ''}_ECustomLogin@i9kkd0`} {...this.props} />;
        }
        if (window.rewritePortalLogin && typeof window.rewritePortalLogin == 'function') {
          return window.rewritePortalLogin({ ...this.props }, { id: layoutUUID });
        }
        return <div />;
      }

      // 使用特色门户布局
      if (customloading && window.weaPortalPublic) {
        const WeaPortalLayout = window.weaPortalPublic.WeaPortalLayout;
        return (
          <WeaPortalLayout
            ecId={`${(this && this.props && this.props.ecId) || ''}_WeaPortalLayout@s4ke32`}
            id={layoutId}
            layoutId={layoutId}
            layoutType={layoutType}
            {...this.props}
          />
        );
      }
      return <div />;
    }

    return <E9Login ecId={`${(this && this.props && this.props.ecId) || ''}_E9Login@7ii7e5`} />;
  }
}

export default Login;
